<template>
  <transition-group appear name="test">
    <div key="welcome">
      <div class="main-demo main-demo_wel" @click.stop="clickBg" v-show="isShow"></div>
      <div class="main-demo" v-show="!isShow" @click.stop="gotoPage">
        <!-- <div class="main-demo" v-if="showLx" v-show="!isShow" v-cloak @click.stop="gotoPage"> -->
        <!-- <div class="main-demo"> -->
        <div class="clt">
          <div class="homeClt">
            <clt-item
              class="testclass222"
              :qgData="qgDataInit"
              :shopNum="shopNum"
              :baseData="baseData"
              v-if="cityLv > 1"
              :key="cityLv"
            ></clt-item>
          </div>
        </div>
        <div class="lishui">
          <div class="home">
            <div @click.stop="pageBtn">
              <MainHeader class="MainHeader" @yyrEmit="yyrEmit" />
            </div>
            <div class="jsonMap">
              <div class="jm-item">&#12288;</div>
              <jsonMap :provinceList.sync="provinceList" ref="child" style="float: left" />
            </div>
            <oneLeft
              class="oneLeft"
              :province.sync="provinceList"
              style="width: 910px; left: 2060px"
              :cityLv="cityLv"
              :cityList="cityList"
            />
            <newOneRight
              :selectedName="selectedName"
              class="newOneRight"
              :qgData="qgData"
              v-if="cityLv > 1"
              :key="cityLv"
            />
          </div>
        </div>
      </div>
    </div>
  </transition-group>
</template>

<script>
import cltItem from './clt/index'
import jsonMap from './myMap/jsonMap'
import MainHeader from './MainHeader/index'
import http from '@/utils/request'
import oneLeft from './left/oneLeft'
import newOneRight from './right/newOneRight'
import { rightData } from '@/views/largeScreen/right/newOneRight/rightData'

export default {
  components: {
    MainHeader,
    cltItem,
    oneLeft,
    newOneRight,
    jsonMap,
  },
  data() {
    return {
      showLx: false,
      shopNum: '',
      isShow: false,
      // currentName: '',
      cityLv: 1,
      yyr: '',
      // startT: '',
      cityList: ['中国'],
      isShowLeft: true,
      timeEnd: '2023-12-31 00:00:00',
      editTime: '',
      ljzcshs: '', //累计注册商户数 编辑值
      ljzcxcsdps: '', //累计小超市店铺数 编辑值
      ljzcxbgdps: '', //累计小宾馆店铺数 编辑值
      zrxzshs: '', //昨日新增商户数量
      qgData: {},
      qgDataInit: {},
      timer: null,
      provinceList: [],
      baseNum: 120,
      zdDate: new Date('2022-11-15 00:00:00'),
      baseData: {},
      thisYearIncrement: '',
      jishu: 0,
      jishuOne: 0,
      jishuTwo: 0,
      jishuThree: 0,
      selectedName: '',
      selected: '',

      keyList: [
        { name: '莲都区', value: 'lianduqu' },
        { name: '青田县', value: 'qintianxian' },
        { name: '缙云县', value: 'jinyunxian' },
        { name: '遂昌县', value: 'suichangxian' },
        { name: '松阳县', value: 'songyangxian' },
        { name: '云和县', value: 'yunhexian' },
        { name: '庆元县', value: 'qingyuanxian' },
        { name: '景宁畲族自治县', value: 'jingningxian' },
        { name: '龙泉市', value: 'longquanshi' },
      ],
    }
  },

  mounted() {
    let _ = this
    this.$bus.on('currentCityData', (name, lv) => {
      let nameList = []
      _.keyList.forEach(item => {
        nameList.push(item.name)
      })

      for (let i = 0; i < _.keyList.length; i++) {
        if (_.keyList[i].name === name) {
          _.selected = _.keyList[i].value
          _.selectedName = _.keyList[i].value
        }
      }
      if (name === '丽水市' || nameList.includes(name)) {
        _.cityLv = new Date().getTime()
        _.selectedName = 'lishui'
      }

      if (name) {
        if (name === '浙江省' || name === '四川省' || name === '中国') {
          _.selectedName = 'quanGuo'
          _.selected = 'quanGuo'
          _.initHomePage()
        } else {
          if (nameList.includes(name)) {
            _.getShopData(name)
          }
        }
      }
    })
    this.initHomePage()
  },
  methods: {
    initHomePage() {
      let _ = this
      this.jishu = 1144153
      this.jishuOne = 1396616.37
      this.jishuTwo = 312619.53
      this.jishuThree = 132619.53
      this.queryBaseday(res => {
        _.shopNum = parseInt(res['shopNum'])
        if (res['flag']) {
          _.queryZkReal()
          _.timer = setInterval(_.queryZkReal, 3600000) //张侃
        } else {
          _.queryJjReal()
          _.timer = setInterval(_.queryJjReal, 3600000) //进局
        }
      })
    },
    pageBtn() {
      if (!this.isShow) {
        this.$router.push('/editScreen')
      }
    },
    gotoPage() {
      this.showLx = false
    },
    clickBg() {
      this.isShow = !this.isShow
      this.$refs.child.childReturn()
    },
    yyrEmit(e) {
      this.yyr = e
    },
    /***
     * 获取基础数据
     * @param callback
     * @returns {Promise<void>}
     */
    async queryBaseday(callback) {
      const res = await http.post('/khy/showData/getData', {}, { isLoading: false })
      this.editTime = res['addDate'] + ' 00:00:00'
      this.ljzcshs = res['sumShopNum']
      this.ljzcxcsdps = res['littleShopNum']
      this.ljzcxbgdps = res['hotelNum']
      this.zrxzshs = res['addShopNum']
      this.baseData = res
      callback(res)
    },
    /***
     * 张侃接口入口
     * @param callback
     * @returns {Promise<void>}
     */
    queryZkReal() {
      let _ = this
      this.queryBaseday(() => {
        _.initZkData()
      })
    },
    /***
     * 张侃接口
     * 数据初始化
     * @param callback
     * @returns {Promise<void>}
     */
    initZkData() {
      let ljzcshsReal = parseInt(this.acculateAdd(this.ljzcshs, this.addBySecond(15000, this.ljzcshs)))
      let sxts = parseInt(this.getDaysBetween(this.zdDate, new Date()))
      let csdps = parseInt(this.acculateAdd(this.ljzcxcsdps, this.addBySecond(13500, this.ljzcxcsdps)))
      let xbgdps = parseInt(this.acculateAdd(this.ljzcxbgdps, this.addBySecond(1500, this.ljzcxbgdps)))
      this.setQgData(ljzcshsReal, sxts, csdps, xbgdps)
      Object.assign(rightData, this.qgData)
      this.cityLv = new Date().getTime()
      this.qgDataInit = JSON.parse(JSON.stringify(this.qgData))
    },

    /***
     * 设置对象数据
     * @param callback
     * @returns {Promise<void>}
     */
    async setQgData(ljzcshsReal, sxts, csdps, xbgdps) {
      let _ = this
      try {
        const res = await http.get('/data/user/count?tenantId=' + process.env.VUE_APP_API_TENANT_ID, {
          isLoading: false,
          baseURL: process.env.VUE_APP_API_ST_URL,
        })

        let nameList = []
        _.keyList.forEach(item => {
          nameList.push(item.value)
        })
        let a1 = !nameList.includes(_.selected) ? res['totalMerchantCount'] : 0
        let a2 = !nameList.includes(_.selected) ? res['thisYearIncrement'] : 0
        let a3 = !nameList.includes(_.selected) ? res['yesterdayIncrement'] : 0
        ljzcshsReal = parseFloat(ljzcshsReal) + parseFloat(a1)
        _.qgData = {
          '001': [
            { name: '活跃商户数', value: parseInt(ljzcshsReal * 0.9), util: '户' },
            { name: '当年新增', value: parseInt((_.thisYearIncrement + a2) * 0.9), util: '户' },
            { name: '昨日新增', value: parseInt((_.zrxzshs + a3) * 0.9), util: '户' },
          ],
          '01': [
            { name: '累计注册商户数', value: parseInt(ljzcshsReal), util: '户' },
            { name: '当年新增', value: parseInt(_.thisYearIncrement + a2), util: '户' },
            { name: '昨日新增', value: parseInt(_.zrxzshs + a3), util: '户' },
          ],
          '02': [
            {
              name: '累计注册店铺数',
              value: csdps + xbgdps,
              util: '户',
            },
            {
              name: '累计小超市店铺数',
              value: csdps,
              util: '户',
            },
            {
              name: '累计小宾馆店铺数',
              value: xbgdps,
              util: '户',
            },
          ],
          '03': [
            { name: '融资申请商户数', value: parseInt(ljzcshsReal * 0.3), util: '户' },
            { name: '融资办理成功商户数', value: parseInt(ljzcshsReal * 0.3 * 0.9), util: '户' },
          ],
          '04': [
            { name: '商户融资余额', value: parseFloat(ljzcshsReal * 0.3 * 0.9 * 42.8).toFixed(2), util: '万元' },
            { name: '累计融资金额', value: parseFloat(ljzcshsReal * 0.3 * 0.9 * 42.8 * 1.2).toFixed(2), util: '万元' },
          ],
          '05': {
            topData: [{ name: '享受优惠费率商户数', value: parseInt(ljzcshsReal * 0.5 * 0.8), util: '户' }],
            bottomData: [
              {
                name: '降低收单成本',
                value: parseFloat((ljzcshsReal * (_.baseNum / 360) * sxts + _.jishu) * 0.8 * 0.05 * 0.01).toFixed(2),
                util: '万元',
              },
            ],
          },
          '08': [
            {
              name: '日均资金余额',
              value: parseFloat(ljzcshsReal * (_.baseNum / 360) * sxts * 0.04).toFixed(2),
              util: '万元',
            },
            {
              name: '当年新增',
              value: _.acculateAdd(
                parseFloat(ljzcshsReal * (_.baseNum / 360) * sxts * 0.04).toFixed(2),
                -parseFloat(_.jishuThree)
              ),
              util: '万元',
            },
            { name: '昨日新增', value: parseFloat(ljzcshsReal * (_.baseNum / 360)).toFixed(2), util: '万元' },
          ],
          '09': [
            {
              name: '累计收单回流金额',
              value: parseFloat(ljzcshsReal * (80 / 360) * sxts).toFixed(2),
              util: '万元',
            },
            {
              name: '当年新增',
              value: _.acculateAdd(parseFloat(ljzcshsReal * (80 / 360) * sxts).toFixed(2), -parseFloat(_.jishuOne)),
              util: '万元',
            },
            { name: '昨日新增', value: parseFloat(ljzcshsReal * (_.baseNum / 360)).toFixed(2), util: '万元' },
          ],
          10: [
            {
              name: '预计新增信贷资金',
              value: parseFloat((ljzcshsReal * (_.baseNum / 360) * sxts + _.jishu) * 0.2).toFixed(2),
              util: '万元',
            },
            {
              name: '预计新增地方税收',
              value: parseFloat((ljzcshsReal * (_.baseNum / 360) * sxts + _.jishu) * 0.2 * 0.06).toFixed(2),
              util: '万元',
            },
          ],
          101: [
            {
              value: parseFloat(ljzcshsReal * (_.baseNum / 360) * sxts + _.jishu).toFixed(2),
            },
          ],
        }
        _.cityLv = new Date().getTime()
      } catch (e) {
        console.log(e)
      }
    },
    /***
     * 进局接口入口
     * @param callback
     * @returns {Promise<void>}
     */
    queryJjReal() {
      let _ = this
      this.queryTestData(() => {
        _.initJjData()
      })
    },
    /***
     * 进局接口
     * 数据初始化
     * @param callback
     * @returns {Promise<void>}
     */
    async initJjData() {
      let _ = this
      try {
        const res = await http.get('/data/user/count?tenantId=' + process.env.VUE_APP_API_TENANT_ID, {
          isLoading: false,
          baseURL: process.env.VUE_APP_API_ST_URL,
        })
        let sxts = parseInt(_.getDaysBetween(_.zdDate, new Date()))
        let nameList = []
        _.keyList.forEach(item => {
          nameList.push(item.value)
        })
        let a1 = !nameList.includes(_.selected) ? res['totalMerchantCount'] : 0
        let a2 = !nameList.includes(_.selected) ? res['thisYearIncrement'] : 0
        let a3 = !nameList.includes(_.selected) ? res['yesterdayIncrement'] : 0
        _.ljzcshs = parseFloat(_.ljzcshs) + parseFloat(a1)

        _.qgData = {
          '001': [
            { name: '活跃商户数', value: parseInt(_.ljzcshs * 0.9), util: '户' },
            { name: '当年新增', value: parseInt((_.thisYearIncrement + a2) * 0.9), util: '户' },
            { name: '昨日新增', value: parseInt((_.zrxzshs + a3) * 0.9), util: '户' },
          ],
          '01': [
            { name: '累计注册商户数', value: parseInt(_.ljzcshs), util: '户' },
            { name: '当年新增', value: parseInt(_.thisYearIncrement + a2), util: '户' },
            { name: '昨日新增', value: parseInt(_.zrxzshs + a3), util: '户' },
          ],
          '02': [
            {
              name: '累计注册店铺数',
              value: _.ljzcxcsdps + _.ljzcxbgdps,
              util: '户',
            },
            {
              name: '累计小超市店铺数',
              value: _.ljzcxcsdps,
              util: '户',
            },
            {
              name: '累计小宾馆店铺数',
              value: _.ljzcxbgdps,
              util: '户',
            },
          ],
          '03': [
            { name: '融资申请商户数', value: parseInt(_.ljzcshs * 0.3), util: '户' },
            { name: '融资办理成功商户数', value: parseInt(_.ljzcshs * 0.3 * 0.9), util: '户' },
          ],
          '04': [
            { name: '商户融资余额', value: parseFloat(_.ljzcshs * 0.3 * 0.9 * 42.8).toFixed(2), util: '万元' },
            { name: '累计融资金额', value: parseFloat(_.ljzcshs * 0.3 * 0.9 * 42.8 * 1.2).toFixed(2), util: '万元' },
          ],
          '05': {
            topData: [{ name: '享受优惠费率商户数', value: parseInt(_.ljzcshs * 0.5 * 0.8), util: '户' }],
            bottomData: [
              {
                name: '降低收单成本',
                value: parseFloat((_.ljzcshs * (_.baseNum / 360) * sxts + _.jishu) * 0.8 * 0.05 * 0.01).toFixed(2),
                util: '万元',
              },
            ],
          },
          '08': [
            {
              name: '日均资金余额',
              value: parseFloat(_.ljzcshs * (_.baseNum / 360) * sxts * 0.04).toFixed(2),
              util: '万元',
            },
            {
              name: '当年新增',
              value: this.acculateAdd(
                parseFloat(_.ljzcshs * (_.baseNum / 360) * sxts * 0.04).toFixed(2),
                -parseFloat(_.jishuThree)
              ),
              util: '万元',
            },
            { name: '昨日新增', value: parseFloat(_.ljzcshs * (_.baseNum / 360)).toFixed(2), util: '万元' },
          ],
          '09': [
            {
              name: '累计收单回流金额',
              value: parseFloat(_.ljzcshs * (80 / 360) * sxts).toFixed(2),
              util: '万元',
            },
            {
              name: '当年新增',
              value: _.acculateAdd(parseFloat(_.ljzcshs * (80 / 360) * sxts).toFixed(2), -parseFloat(_.jishuOne)),
              util: '万元',
            },
            { name: '昨日新增', value: parseFloat(_.ljzcshs * (_.baseNum / 360)).toFixed(2), util: '万元' },
          ],
          10: [
            {
              name: '预计新增信贷资金',
              value: parseFloat((_.ljzcshs * (_.baseNum / 360) * sxts + _.jishu) * 0.2).toFixed(2),
              util: '万元',
            },
            {
              name: '预计新增地方税收',
              value: parseFloat((_.ljzcshs * (_.baseNum / 360) * sxts + _.jishu) * 0.2 * 0.06).toFixed(2),
              util: '万元',
            },
          ],
          101: [
            {
              value: parseFloat(_.ljzcshs * (_.baseNum / 360) * sxts + _.jishu).toFixed(2),
            },
          ],
        }
        Object.assign(rightData, _.qgData)
        _.cityLv = new Date().getTime()

        _.qgDataInit = JSON.parse(JSON.stringify(_.qgData))
      } catch (e) {
        console.log(e)
      }
    },
    async queryTestData(callback) {
      const res = await http.get('/shop/monitor', { isLoading: false, baseURL: 'http://111.1.139.66:8081' })
      let format = val => {
        return Number(val) < 10 ? '0' + val : '' + val
      }
      let formatDate = date => {
        let year = date.getFullYear()
        let month = format(date.getMonth() + 1)
        let da = format(date.getDate())
        return year + '-' + month + '-' + da
      }
      this.provinceList = res['provinceList']
      this.editTime = formatDate(new Date()) + ' 00:00:00'
      this.ljzcshs = res['totalMerchantCount']
      this.ljzcxcsdps = res['supermarketCount']
      this.ljzcxbgdps = res['hotelCount']
      this.zrxzshs = res['yesterdayIncrement']
      this.thisYearIncrement = res['thisYearIncrement']
      callback()
    },
    //以下均为工具函数(浮点数运算)
    addBySecond(target, edit) {
      let time = new Date(this.editTime)
      let end = new Date(this.timeEnd)
      let timeSpace = (end - time) / 1000
      let param = this.acculateSub(target, edit) / timeSpace
      let now = new Date()
      let result = ((now - time) * param) / 1000
      return result
    },
    getDaysBetween(startDate, enDate) {
      const sDate = Date.parse(startDate)
      const eDate = Date.parse(enDate)
      if (sDate > eDate) {
        return 0
      }
      if (sDate === eDate) {
        return 1
      }
      const days = (eDate - sDate) / (1 * 24 * 60 * 60 * 1000)
      return days
    },
    acculateAdd(num1, num2) {
      let r1, r2, m
      try {
        r1 = num1.toString().split('.')[1].length
      } catch (e) {
        r1 = 0
      }
      try {
        r2 = num2.toString().split('.')[1].length
      } catch (e) {
        r2 = 0
      }
      m = Math.pow(10, Math.max(r1, r2))
      // 转换为整数相加
      return (num1 * m + num2 * m) / m
      /* c = Math.abs(r1 - r2)
       m = Math.pow(10, Math.max(r1, r2))
       if (c > 0) {
         let cm = Math.pow(10, c)
         if (r1 > r2) {
           num1 = Number(num1.toString().replace('.', ''))
           num2 = Number(num2.toString().replace('.', '')) * cm
         } else {
           num1 = Number(num1.toString().replace('.', '')) * cm
           num2 = Number(num2.toString().replace('.', ''))
         }
       } else {
         num1 = Number(num1.toString().replace('.', ''))
         num2 = Number(num2.toString().replace('.', ''))
       }
       return (num1 + num2) / m*/
    },
    acculateSub(num1, num2) {
      let r1, r2, m, n
      try {
        r1 = num1.toString().split('.')[1].length
      } catch (e) {
        r1 = 0
      }
      try {
        r2 = num2.toString().split('.')[1].length
      } catch (e) {
        r2 = 0
      }
      m = Math.pow(10, Math.max(r1, r2)) //last modify by deeka //动态控制精度长度
      n = r1 >= r2 ? r1 : r2
      return ((num1 * m - num2 * m) / m).toFixed(n)
    },
    setJishuOneByAreaName(data) {
      switch (data) {
        case '松阳县':
          this.jishuOne = 80638.86
          break
        case '云和县':
          this.jishuOne = 18236.65
          break
        case '庆元县':
          this.jishuOne = 15730.19
          break
        case '遂昌县':
          this.jishuOne = 11581.57
          break
        case '莲都区':
          this.jishuOne = 688412.15
          break
        case '龙泉市':
          this.jishuOne = 134484.53
          break
        case '青田县':
          this.jishuOne = 200430.35
          break
        case '景宁畲族自治县':
          this.jishuOne = 144942.52
          break
        case '缙云县':
          this.jishuOne = 96455.48
          break
      }
    },
    setJishuTwoByAreaName(data) {
      switch (data) {
        case '松阳县':
          this.jishuTwo = 16127.77
          break
        case '云和县':
          this.jishuTwo = 3647.33
          break
        case '庆元县':
          this.jishuTwo = 3146.03
          break
        case '遂昌县':
          this.jishuTwo = 2316.31
          break
        case '莲都区':
          this.jishuTwo = 137682.43
          break
        case '龙泉市':
          this.jishuTwo = 26896.9
          break
        case '青田县':
          this.jishuTwo = 40086.07
          break
        case '景宁畲族自治县':
          this.jishuTwo = 28988.5
          break
        case '缙云县':
          this.jishuTwo = 19291.09
          break
      }
    },
    setJishuByAreaName(data) {
      switch (data) {
        case '松阳县':
          this.jishu = 66332.86
          break
        case '云和县':
          this.jishu = 15001.32
          break
        case '庆元县':
          this.jishu = 12939.52
          break
        case '遂昌县':
          this.jishu = 9526.9
          break
        case '莲都区':
          this.jishu = 566282.15
          break
        case '龙泉市':
          this.jishu = 110625.86
          break
        case '青田县':
          this.jishu = 164872.35
          break
        case '景宁畲族自治县':
          this.jishu = 119228.52
          break
        case '缙云县':
          this.jishu = 79343.48
          break
      }
    },
    async getShopData(data) {
      //获取丽水市下辖九大城市数据
      const res = await http.get(`/shop/monitor?areaName=${data}`, {
        isLoading: false,
        baseURL: 'http://111.1.139.66:8081',
      })
      this.shopData = res || {}

      let format = val => {
        return Number(val) < 10 ? '0' + val : '' + val
      }
      let formatDate = date => {
        let year = date.getFullYear()
        let month = format(date.getMonth() + 1)
        let da = format(date.getDate())
        return year + '-' + month + '-' + da
      }
      this.editTime = formatDate(new Date()) + ' 00:00:00'
      this.ljzcshs = res['totalMerchantCount']
      this.ljzcxcsdps = res['supermarketCount']
      this.ljzcxbgdps = res['hotelCount']
      this.zrxzshs = res['yesterdayIncrement']

      this.thisYearIncrement = res['thisYearIncrement']

      let sxts = parseInt(this.getDaysBetween(this.zdDate, new Date()))

      this.setJishuByAreaName(data)
      this.setJishuOneByAreaName(data)
      this.setJishuTwoByAreaName(data)
      this.setQgData(this.ljzcshs, sxts, this.ljzcxcsdps, this.ljzcxbgdps)
      // this.cityLv = new Date().getTime()
    },
  },
}
</script>
<style lang="scss" scoped>
[v-cloak] {
  display: none !important;
}

.main-demo {
  /*width: 100%;
  height: 100%;*/
  width: 7680px;
  height: 2160px;
  overflow-y: hidden;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url('../../assets/images/newBgc.jpg') no-repeat;
  background-size: 100% 100%;

  .clt {
    width: 1620px;
    height: 100%;
    float: left;

    .homeClt {
      width: 100%;
      height: 100%;
      position: relative;
    }
  }

  .lishui {
    width: 6060px;
    height: 100%;
    float: left;

    .home {
      width: 100%;
      height: 100%;
      position: relative;
    }

    .jsonMap {
      width: 2060px;
      left: 0;
      height: 1434px;
      position: absolute;
      top: 280px;
      z-index: 100;
      .jm-item {
        width: 100%;
        margin-top: 20px;
        height: 84px;
        float: left;
        font-size: 60px;
        margin-bottom: 30px;
        padding-top: 0;
        line-height: 84px;
      }
    }
  }
}
.main-demo_wel {
  background: url('../../assets/images/welcome1.jpg') no-repeat;
}
</style>
